#include "addstu.h"
#include "ui_addstu.h"


#include <QCheckBox>
#include <QMessageBox>
#include "mainwindow.h"

#include <QTextStream>
#include <QDebug>
#include <QFile>
AddStu::AddStu(QWidget *parent )
    : QDialog(parent)
    , ui(new Ui::AddStu)
{
    ui->setupUi(this);
}

AddStu::~AddStu()
{
    delete ui;
}


void AddStu::on_pushButton_2_clicked()
{
    this->close();
}

void AddStu::on_pushButton_clicked()
{
    //判断姓名，学号，和兴趣是否为空，如果为空，弹出错误框
    QString name = this->ui->nameEdit->text();
    QString sno = this->ui->snoEdit->text();

    QList<QAbstractButton *> checkBoxes = this->ui->checkGroup->buttons();
    QList<QAbstractButton *> sexBoxes = this->ui->sexGroup->buttons();


    QString content = "";
    if(name.length()<1 || sno.length()<5 || checkBoxes.length()<1)
    {
        QMessageBox::critical(this,"错误信息","信息不完整，请重新检查","确定");
    }else
    {
        //符合条件，将所有数据获取到
        //姓名，学号，性别，年龄，院系，兴趣
        content = this->getContent();

        //将content显示到QMessageBox
        int ret = QMessageBox::question(this,"详细信息",content,"确定","取消");

        //保存成功，
        if(ret==0)
        {
            QMessageBox::information(this,"信息","保存成功!","确定");
            QString line_str = content.replace(","," ").replace("\n"," ");
            qDebug()<<line_str<<endl;
            this->save2File(line_str);
            //清除界面，进行下次添加
            this->ClearInterface();
        }
    }
}

int AddStu::save2File(QString content)
{
    QFile file("stu.txt");
    if(!file.open(QIODevice::Append|QIODevice::Text))
    {
        return -1;
    }

    QTextStream out(&file);
    out<<content<<"\n";
    file.close();
    return 1;

}
QString AddStu::getContent()
{
    QString name = this->ui->nameEdit->text();
    QString sno = this->ui->snoEdit->text();

    QList<QAbstractButton *> checkBoxes = this->ui->checkGroup->buttons();
    QList<QAbstractButton *> sexBoxes = this->ui->sexGroup->buttons();


    QString content = "";

    content.append(name+"\n");
    content.append(sno+"\n");
    for(int i = 0;i<sexBoxes.length();i++)
    {
        QAbstractButton * sexbtn = sexBoxes.at(i);
        if(sexbtn->isChecked())
        {
            content.append(sexbtn->text()+"\n");
        }
    }
    content.append(this->ui->cbx_age->currentText()+"\n");
    content.append(this->ui->cbx_college->currentText()+"\n");
    for(int i = 0;i<checkBoxes.length()-1;i++)
    {
        QAbstractButton *insbtn = checkBoxes.at(i);
        if(insbtn->isChecked())
        {
            content.append(insbtn->text()+",");
        }
    }

    content.remove(content.length()-1,1); //删除最后的逗号

    return content;
}
void AddStu::ClearInterface()
{
    //清除姓名，学号，性别归男，年龄和院系回归默认
    this->ui->nameEdit->clear();
    this->ui->snoEdit->clear();
    this->ui->btn_female->setChecked(true);

    this->ui->cbx_age->setCurrentIndex(0);
    this->ui->cbx_college->setCurrentIndex(0);

   QList<QAbstractButton *> checkBoxes =  this->ui->checkGroup->buttons();
   for(int i = 0;i<checkBoxes.length();i++)
   {
        checkBoxes.at(i)->setChecked(false);
   }

    //焦点回归姓名
    this->ui->nameEdit->setFocus();
}
